/* I (David P Shenba) license this file to you under the Apache License, 
 * Version 2.0 (the "License"); you may not use this 
 * file except in compliance with the License.  You may 
 * obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.shenba.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.shenba.beans.InnerBean;
import org.shenba.beans.OuterBean;
import org.shenba.forms.DemoForm;

/**
 * @author shenba
 *
 */
public class DemoAction extends DispatchAction {
	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward showBlankPage(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		DemoForm demoForm = (DemoForm) form;
		int innerCount = 3, outerCount = 2, i, j;
		ArrayList<OuterBean> outerBeans = new ArrayList<OuterBean>();
		for (i = 0; i < outerCount; i++) {
			OuterBean ob = new OuterBean();
			ob.setField1("dummy"+i);
			ob.setField2("any"+i);
			ArrayList<InnerBean> innerBeans = new ArrayList<InnerBean>();
			for (j = 0; j < innerCount; j++) {
				InnerBean ib = new InnerBean();
				ib.setField1("some"+j);
				ib.setField2("test"+j);
				innerBeans.add(ib);
			}
			ob.setInnerBeans(innerBeans);
			outerBeans.add(ob);
		}
		demoForm.setOuterBeans(outerBeans);
		return mapping.findForward("entry");
	}
	/**
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward displayBeans(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		DemoForm demoForm = (DemoForm) form;
		//just for inspecting the list in debug mode
		List<OuterBean> outerBeans = demoForm.getOuterBeans();
 		if(outerBeans ==  null) {
			demoForm.setOuterBeans(new ArrayList<OuterBean>());
		}
		//your save logic call goes here
		//your retrieval logic goes here
		return mapping.findForward("display");
	}
}
